package org.wu.framework.authorization.platform.infrastructure.persistence;

import org.wu.framework.authorization.platform.infrastructure.entity.LazyZoneApplicationDO;
import org.wu.framework.authorization.platform.infrastructure.converter.LazyZoneApplicationConverter;
import org.wu.framework.authorization.platform.domain.model.lazy.zone.application.LazyZoneApplicationRepository;
import org.springframework.stereotype.Repository;

import java.util.stream.Collectors;

import org.wu.framework.lazy.orm.database.lambda.stream.wrapper.LazyWrappers;
import org.wu.framework.web.response.Result;
import org.wu.framework.web.response.ResultFactory;
import jakarta.annotation.Resource;
import org.wu.framework.authorization.platform.domain.model.lazy.zone.application.LazyZoneApplication;
import org.wu.framework.lazy.orm.database.lambda.stream.lambda.LazyLambdaStream;

import java.util.List;

import org.wu.framework.lazy.orm.database.lambda.domain.LazyPage;

/**
 * describe 应用信息
 *
 * @author Jia wei Wu
 * @date 2024/04/02 06:21 晚上
 * @see org.wu.framework.lazy.orm.core.persistence.reverse.lazy.ddd.DefaultDDDLazyInfrastructurePersistence
 **/
@Repository
public class LazyZoneApplicationRepositoryImpl implements LazyZoneApplicationRepository {

    @Resource
    LazyLambdaStream lazyLambdaStream;

    /**
     * describe 新增应用信息
     *
     * @param lazyZoneApplication 新增应用信息
     * @return {@link Result<LazyZoneApplication>} 应用信息新增后领域对象
     * @author Jia wei Wu
     * @date 2024/04/02 06:21 晚上
     **/

    @Override
    public Result<LazyZoneApplication> story(LazyZoneApplication lazyZoneApplication) {
        LazyZoneApplicationDO lazyZoneApplicationDO = LazyZoneApplicationConverter.INSTANCE.fromLazyZoneApplication(lazyZoneApplication);
        lazyLambdaStream.upsert(lazyZoneApplicationDO);
        return ResultFactory.successOf();
    }

    /**
     * describe 批量新增应用信息
     *
     * @param lazyZoneApplicationList 批量新增应用信息
     * @return {@link Result<List<LazyZoneApplication>>} 应用信息新增后领域对象集合
     * @author Jia wei Wu
     * @date 2024/04/02 06:21 晚上
     **/

    @Override
    public Result<List<LazyZoneApplication>> batchStory(List<LazyZoneApplication> lazyZoneApplicationList) {
        List<LazyZoneApplicationDO> lazyZoneApplicationDOList = lazyZoneApplicationList.stream().map(LazyZoneApplicationConverter.INSTANCE::fromLazyZoneApplication).collect(Collectors.toList());
        lazyLambdaStream.upsert(lazyZoneApplicationDOList);
        return ResultFactory.successOf();
    }

    /**
     * describe 查询单个应用信息
     *
     * @param lazyZoneApplication 查询单个应用信息
     * @return {@link Result<LazyZoneApplication>} 应用信息领域对象
     * @author Jia wei Wu
     * @date 2024/04/02 06:21 晚上
     **/

    @Override
    public Result<LazyZoneApplication> findOne(LazyZoneApplication lazyZoneApplication) {
        LazyZoneApplicationDO lazyZoneApplicationDO = LazyZoneApplicationConverter.INSTANCE.fromLazyZoneApplication(lazyZoneApplication);
        LazyZoneApplication lazyZoneApplicationOne = lazyLambdaStream
                .selectOne(
                        LazyWrappers.lambdaWrapperBean(lazyZoneApplicationDO).eq(LazyZoneApplicationDO::getIsDeleted, false)
                        , LazyZoneApplication.class);
        return ResultFactory.successOf(lazyZoneApplicationOne);
    }

    /**
     * describe 查询多个应用信息
     *
     * @param lazyZoneApplication 查询多个应用信息
     * @return {@link Result<List<LazyZoneApplication>>} 应用信息领域对象
     * @author Jia wei Wu
     * @date 2024/04/02 06:21 晚上
     **/

    @Override
    public Result<List<LazyZoneApplication>> findList(LazyZoneApplication lazyZoneApplication) {
        LazyZoneApplicationDO lazyZoneApplicationDO = LazyZoneApplicationConverter.INSTANCE.fromLazyZoneApplication(lazyZoneApplication);
        List<LazyZoneApplication> lazyZoneApplicationList = lazyLambdaStream
                .selectList(
                        LazyWrappers.lambdaWrapperBean(lazyZoneApplicationDO)
                                .eq(LazyZoneApplicationDO::getIsDeleted, false)
                        , LazyZoneApplication.class);
        return ResultFactory.successOf(lazyZoneApplicationList);
    }

    /**
     * describe 分页查询多个应用信息
     *
     * @param size                当前页数
     * @param current             当前页
     * @param lazyZoneApplication 分页查询多个应用信息
     * @return {@link Result<LazyPage<LazyZoneApplication>>} 分页应用信息领域对象
     * @author Jia wei Wu
     * @date 2024/04/02 06:21 晚上
     **/

    @Override
    public Result<LazyPage<LazyZoneApplication>> findPage(int size, int current, LazyZoneApplication lazyZoneApplication) {
        LazyZoneApplicationDO lazyZoneApplicationDO = LazyZoneApplicationConverter.INSTANCE.fromLazyZoneApplication(lazyZoneApplication);
        LazyPage<LazyZoneApplication> lazyPage = new LazyPage<>(current, size);
        LazyPage<LazyZoneApplication> lazyZoneApplicationLazyPage = lazyLambdaStream
                .selectPage(
                        LazyWrappers.lambdaWrapperBean(lazyZoneApplicationDO)
                                .eq(LazyZoneApplicationDO::getIsDeleted, false),
                        lazyPage,
                        LazyZoneApplication.class);
        return ResultFactory.successOf(lazyZoneApplicationLazyPage);
    }

    /**
     * describe 删除应用信息
     *
     * @param lazyZoneApplication 删除应用信息
     * @return {@link Result<LazyZoneApplication>} 应用信息
     * @author Jia wei Wu
     * @date 2024/04/02 06:21 晚上
     **/

    @Override
    public Result<LazyZoneApplication> remove(LazyZoneApplication lazyZoneApplication) {
        LazyZoneApplicationDO updateEntity = new LazyZoneApplicationDO();
        updateEntity.setIsDeleted(true);
        lazyLambdaStream.update(updateEntity, LazyWrappers.<LazyZoneApplicationDO>lambdaWrapper()
                .eq(LazyZoneApplicationDO::getId,lazyZoneApplication.getId()));
        return ResultFactory.successOf();
    }

    /**
     * describe 是否存在应用信息
     *
     * @param lazyZoneApplication 应用信息领域对象
     * @return {@link Result<Boolean>} 是否存在 true 存在，false 不存在
     * @author Jia wei Wu
     * @date 2024/04/02 06:21 晚上
     **/

    @Override
    public Result<Boolean> exists(LazyZoneApplication lazyZoneApplication) {
        LazyZoneApplicationDO lazyZoneApplicationDO = LazyZoneApplicationConverter.INSTANCE.fromLazyZoneApplication(lazyZoneApplication);
        Boolean exists = lazyLambdaStream.exists(LazyWrappers.lambdaWrapperBean(lazyZoneApplicationDO));
        return ResultFactory.successOf(exists);
    }

}